﻿Imports BUS, DTO
Public Class frmcapnhatsach
    Private Sub LoadDSSach()
        Dim dsSach As New List(Of SachDTO)
        Dim bus As New SachBUS()
        dsSach = bus.LayDSSach()
        dtgvDSSach.Rows.Clear()

        For i As Integer = 0 To dsSach.Count - 1
            dtgvDSSach.Rows.Add(dsSach(i).MaSach, dsSach(i).TenSach, dsSach(i).MaTheLoai, dsSach(i).MaTacGia, dsSach(i).NamXuatBan, dsSach(i).MaNhaSanXuat, dsSach(i).NgayNhap, dsSach(i).TriGia, dsSach(i).MaNhanVien, dsSach(i).MaTinhTrang)
        Next
    End Sub

    Private Sub frmcapnhatsach_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadDSSach()
    End Sub

    Private Sub btncapnhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncapnhat.Click
        Dim fr As New frmxlcapnhatsach()
        Dim index As Integer = dtgvDSSach.CurrentRow.Index

        If index >= 0 Then
            fr.MaSach = dtgvDSSach.Item(0, index).Value.ToString()
            fr.txttensach.Text = dtgvDSSach.Item(1, index).Value.ToString()

            Dim tlBUS As New LoaiSachBUS()
            Dim dsTL As New List(Of LoaiSachDTO)
            dsTL = tlBUS.LayDSLoaiSach()
            fr.LookUptheloai.Properties.DataSource = dsTL
            fr.LookUptheloai.Properties.ValueMember = "MaTheLoai"
            fr.LookUptheloai.Properties.DisplayMember = "TenTheLoai"
            fr.LookUptheloai.EditValue = dtgvDSSach.Item(2, index).Value

            Dim tgBUS As New TacGiaBUS()
            Dim dsTG As New List(Of TacGiaDTO)
            dsTG = tgBUS.LayDSTacGia()
            fr.LookUpEdit_TacGia.Properties.DataSource = dsTG
            fr.LookUpEdit_TacGia.Properties.DisplayMember = "TenTacGia"
            fr.LookUpEdit_TacGia.Properties.ValueMember = "MaTacGia"
            fr.LookUpEdit_TacGia.EditValue = dtgvDSSach.Item(3, index).Value.ToString()

            Dim nxbBUS As New NhaXuatBanBUS()
            Dim dsNXB As New List(Of NhaXuatBanDTO)
            dsNXB = nxbBUS.LayDSNhaSanXuat()
            fr.LookUpEdit_NhaXuatBan.Properties.DataSource = dsNXB
            fr.LookUpEdit_NhaXuatBan.Properties.ValueMember = "MaNhaSanXuat"
            fr.LookUpEdit_NhaXuatBan.Properties.DisplayMember = "TenNhaSanXuat"
            fr.LookUpEdit_NhaXuatBan.EditValue = dtgvDSSach.Item(5, index).Value.ToString()

            Dim ttBUS As New TinhTrangBUS()
            Dim dsTT As New List(Of TinhTrangDTO)
            dsTT = ttBUS.LayDSTinhTrang()
            fr.LookUpEdit_TinhTrangSach.Properties.DataSource = dsTT
            fr.LookUpEdit_TinhTrangSach.Properties.ValueMember = "MaTinhTrangSach"
            fr.LookUpEdit_TinhTrangSach.Properties.DisplayMember = "TenTinhTrangSach"
            fr.LookUpEdit_TinhTrangSach.EditValue = dtgvDSSach.Item(9, index).Value.ToString()

            fr.SpinEdit_NamXuatBan.Value = dtgvDSSach.Item(4, index).Value.ToString()
            fr.DateEditngaynhap.EditValue = dtgvDSSach.Item(6, index).Value.ToString()
            fr.SpinEdit_TriGia.Value = dtgvDSSach.Item(7, index).Value.ToString()

            Dim nvBUS As New NhanVienBUS()
            Dim dsnv As New List(Of NhanVienDTO)
            dsnv = nvBUS.LayDSNhanVien_ThuKho()
            fr.LookUpnguoitiepnhan.Properties.DataSource = dsnv
            fr.LookUpnguoitiepnhan.Properties.DisplayMember = "HoTen"
            fr.LookUpnguoitiepnhan.Properties.ValueMember = "MaNhanVien"
            fr.LookUpnguoitiepnhan.EditValue = dtgvDSSach.Item(8, index).Value.ToString()

        End If
        fr.ShowDialog()

        If fr.DialogResult = Windows.Forms.DialogResult.OK Then
            LoadDSSach()
        End If

    End Sub
End Class